00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef BASEEXPORT_HPP_
00017 #define BASEEXPORT_HPP_
00018
00019 #include <iostream>
00020 #include <string>
00021 #include <vector>
00022 #include <map>
00023
00024 #include "gridpack/parallel/communicator.hpp"
00025 #include "gridpack/network/base_network.hpp"
00026
00027 #define MAX_STRING_SIZE 1024
00028
00029 namespace gridpack {
00030 namespace expnet {
00031
00032
00033 struct text_line {
00034
00035 int global_idx;
00036
00037 int device_idx;
00038
00039 char text[MAX_STRING_SIZE];
00040 };
00041
00042 template<class _network>
00043 class BaseExport
00044 {
00045 public:
00046
00047
00048
00049
00050 explicit BaseExport(gridpack::parallel::Communicator comm) : p_comm(comm)
00051 {
00052 }
00053
00054
00055
00056
00057 virtual ~BaseExport(){}
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void writeDataBlock(std::ofstream &fout, std::vector<text_line> &text_data)
00068 {
00069
00070
00071 int i;
00072 int nsize = text_data.size();
00073 int gmax = -1;
00074 for (i=0; i<nsize; i++) {
00075 if (text_data[i].global_idx > gmax) gmax = text_data[i].global_idx;
00076 }
00077 gmax++;
00078 p_comm.max(&gmax,1);
00079 if (gmax == 0) return;
00080
00081 int grp = p_comm.getGroup();
00082 int one = 1;
00083 int ttype = NGA_Register_type(sizeof(text_line));
00084 int g_offset = NGA_Create_handle();
00085 if (gmax == 0) return;
00086 NGA_Set_data(g_offset,one,&gmax,C_INT);
00087 NGA_Set_pgroup(g_offset,grp);
00088 NGA_Allocate(g_offset);
00089 NGA_Zero(g_offset);
00090
00091
00092 std::vector<int*> indices;
00093 indices.resize(nsize);
00094 std::vector<int> ones;
00095 ones.resize(nsize);
00096 for (i=0; i<nsize; i++) {
00097 indices[i] = &text_data[i].global_idx;
00098 ones[i] = one;
00099 }
00100 if (nsize > 0) {
00101 NGA_Scatter_acc(g_offset,&ones[0],&indices[0],nsize,&one);
00102 }
00103 GA_Pgroup_sync(grp);
00104
00105
00106
00107 int lo,hi;
00108 int me = p_comm.rank();
00109 int nproc = p_comm.size();
00110 NGA_Distribution(g_offset,me,&lo,&hi);
00111 int nelem = hi-lo+1;
00112 int ld;
00113 int *ptr;
00114 if (nelem > 0) {
00115 NGA_Access(g_offset,&lo,&hi,&ptr,&ld);
00116 }
00117 std::vector<int> offsets;
00118 offsets.resize(nelem);
00119 if (nelem > 0) offsets[0] = 0;
00120 int tmp;
00121 int total = 0;
00122 if (nelem > 0) total = ptr[0];
00123 for (i=1; i<nelem; i++) {
00124 offsets[i] = offsets[i-1]+ptr[i-1];
00125 total += ptr[i];
00126 }
00127 for (i=0; i<nelem; i++) {
00128 ptr[i] = offsets[i];
00129 }
00130
00131 std::vector<int> block_sizes;
00132 block_sizes.resize(nproc);
00133 for (i=0; i<nproc; i++) block_sizes[i] = 0;
00134 block_sizes[me] = total;
00135 p_comm.sum(&block_sizes[0],nproc);
00136 int loffset = 0;
00137 for (i=0; i<me; i++) loffset += block_sizes[i];
00138 total = 0;
00139 for (i=0; i<nproc; i++) total += block_sizes[i];
00140
00141 for (i=0; i<nelem; i++) ptr[i] = ptr[i]+loffset;
00142 if (nelem > 0) {
00143 NGA_Release(g_offset,&lo,&hi);
00144 }
00145
00146
00147 int g_txt = NGA_Create_handle();
00148 NGA_Set_pgroup(g_txt,grp);
00149 NGA_Set_data(g_txt,one,&total,ttype);
00150 NGA_Allocate(g_txt);
00151
00152 std::vector<int> tidx;
00153 tidx.resize(nsize);
00154 NGA_Gather(g_offset,&tidx[0],&indices[0],nsize);
00155
00156
00157 for (i=0; i<nsize; i++) {
00158 tidx[i] = tidx[i] + text_data[i].device_idx;
00159 indices[i] = &tidx[i];
00160 }
00161
00162 NGA_Scatter(g_txt,&text_data[0],&indices[0],nsize);
00163 NGA_Pgroup_sync(grp);
00164
00165
00166 if (me == 0) {
00167 int iproc;
00168 for (iproc=0; iproc<nproc; iproc++) {
00169 NGA_Distribution(g_txt,iproc,&lo,&hi);
00170 std::vector<text_line> lines;
00171 int nlines = hi-lo+1;
00172 lines.resize(nlines);
00173 if (nlines > 0) NGA_Get(g_txt,&lo,&hi,&lines[0],&one);
00174 for (i=0; i<nlines; i++) {
00175 fout << lines[i].text;
00176 }
00177 }
00178 }
00179
00180 NGA_Destroy(g_offset);
00181 NGA_Destroy(g_txt);
00182 NGA_Deregister_type(ttype);
00183 }
00184
00185 private:
00186 boost::shared_ptr<_network> p_network;
00187
00188 gridpack::parallel::Communicator p_comm;
00189 };
00190
00191 }
00192 }
00193
00194 #endif